package org.jwos.plugin.file.jcr.config;

import java.util.Iterator;
import java.util.Properties;

import org.apache.commons.collections.BeanMap;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BootstrapConfig {
	
	private static final Logger log = LoggerFactory.getLogger(BootstrapConfig.class);
	
	private BeanMap map = new BeanMap(this);

    private String repositoryHome;

    private String repositoryConfig;

    private String repositoryName;

    private Properties jndiEnv = new Properties();
    
    public BootstrapConfig() {    	
    }
    
    public void init(Properties props) {
        Iterator iter = props.keySet().iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next();
            String mapName = toMapName(name, '.');
            if (map.containsKey(mapName)) {
                map.put(mapName, props.getProperty(name));
            }
            if (name.startsWith("java.naming.")) {
                jndiEnv.put(name, props.getProperty(name));
            }            
        }
    }
    
    public String toMapName(String name, char delim) {
        StringBuffer ret = new StringBuffer();
        String[] elems = Text.explode(name, delim);
        ret.append(elems[0]);
        for (int i=1; i<elems.length; i++) {
            ret.append(elems[i].substring(0, 1).toUpperCase());
            ret.append(elems[i].substring(1));
        }
        return ret.toString();
    }
    
    public String getJndiName() {
    	return repositoryName;
    }

    public String getRepositoryHome() {
        return repositoryHome;
    }

    public void setRepositoryHome(String repositoryHome) {
        this.repositoryHome = repositoryHome;
    }

    public String getRepositoryConfig() {
        return repositoryConfig;
    }

    public void setRepositoryConfig(String repositoryConfig) {
        this.repositoryConfig = repositoryConfig;
    }

    public String getRepositoryName() {
        return repositoryName;
    }

    public void setRepositoryName(String repositoryName) {
        this.repositoryName = repositoryName;
    }

	public Properties getJndiEnv() {
		return jndiEnv;
	}

	public void setJndiEnv(Properties jndiEnv) {
		this.jndiEnv = jndiEnv;
	}    
}